package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.enums.HuanXinMessageType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Reference(version = "1.0.0")
    private HuanXinApi huanXinApi;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 获取陌生人的问题
     *
     * @param userId
     * @return
     */
    public String getQuestion(Integer userId) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", Convert.toLong(userId));
        Question question = questionMapper.selectOne(wrapper);
        if (question != null) {
            return question.getTxt();
        }
        return "您平时有什么爱好呢,亲?";
    }

    /**
     * 回答陌生人的问题
     *
     * @param userId
     * @param reply
     * @return
     */
    public Boolean postQuestion(Long userId, String reply) {
/*        User user = UserThreadLocal.get();msrId
        Long userId = user.getId();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("huanxinId","HX_" + userId);
        map.put("nickname",userInfo.getNickName());
        map.put("strangerQuestion",this.getQuestion(msrId.intValue()));
        map.put("reply", reply);
        Boolean aBoolean = huanXinApi.sendMsgFromAdmin("HX_"+msrId, HuanXinMessageType.TXT, JSONUtil.toJsonStr(map));
        return aBoolean;*/

        User user = UserThreadLocal.get();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);


        //构建消息内容
        Map<String, Object> msg = new HashMap<>();
        msg.put("userId", user.getId());
        msg.put("huanXinId", "HX_" + user.getId());
        msg.put("nickname", userInfo.getNickName());
        msg.put("strangerQuestion", this.getQuestion(Convert.toInt(userId)));
        msg.put("reply", reply);

        //发送环信消息
        return this.huanXinApi.sendMsgFromAdmin("HX_" + userId,
                HuanXinMessageType.TXT, JSONUtil.toJsonStr(msg));


    }
}
